package com.nisco.dms.entity;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.google.common.base.Objects;
import com.google.common.collect.Lists;

@Entity
@Table(name = "Ds_Menu")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = "com.nisco.dms.entity")
public class Menu extends CoreEntity implements Comparable<Menu> {

	@Column
	private String strSn;

	@Column
	private String strName;

	@Column
	private String strDesc;

	/**
	 * 模块的排序号,越小优先级越高
	 */
	@Column
	private Integer intPriority = 99;
	

	@ManyToOne
	@JoinColumn(name = "fatherId")
	private Menu parent;

	@OneToMany(cascade = CascadeType.PERSIST, mappedBy = "parent")
	@OrderBy("intPriority ASC")
	private List<Menu> children = Lists.newArrayList();
	@ManyToOne
	@JoinColumn(name="programId")
	private Program program;


	public String getStrSn()
	{
		return strSn;
	}


	public void setStrSn(String strSn)
	{
		this.strSn = strSn;
	}


	public String getStrName()
	{
		return strName;
	}


	public void setStrName(String strName)
	{
		this.strName = strName;
	}



	public String getStrDesc()
	{
		return strDesc;
	}


	public void setStrDesc(String strDesc)
	{
		this.strDesc = strDesc;
	}


	
	

	public Integer getIntPriority()
	{
		return intPriority;
	}


	public void setIntPriority(Integer intPriority)
	{
		this.intPriority = intPriority;
	}


	public Menu getParent()
	{
		return parent;
	}


	public void setParent(Menu parent)
	{
		this.parent = parent;
	}


	public List<Menu> getChildren()
	{
		return children;
	}


	public void setChildren(List<Menu> children)
	{
		this.children = children;
	}
	
	

	public Program getProgram()
	{
		return program;
	}


	public void setProgram(Program program)
	{
		this.program = program;
	}


	/*
	 * 
	 */
	public int compareTo(Menu m) {
		if (m == null) {
			return -1;
		} else if (m == this) {
			return 0;
		} else if (this.intPriority < m.getIntPriority()) {
			return -1;
		} else if (this.intPriority > m.getIntPriority()) {
			return 1;
		}

		return 0;
	}
	@Override
	public String toString() {
		return Objects.toStringHelper(this).addValue(id).addValue(strName).addValue(parent == null ? null : parent.getStrName()).addValue(children.size()).toString();
	}

}
